package cn.sumpu.app.guoguojie.biz.impl.intercept;

import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.taobao.top.web.Config;

import cn.sumpu.app.guoguojie.biz.CmsContentBiz;
import cn.sumpu.app.guoguojie.biz.MemberBiz;
import cn.sumpu.app.guoguojie.domain.CmsContent;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.OnLineUserListener;
import cn.sumpu.app.guoguojie.util.Common;

public class AllFilter implements HandlerInterceptor {

    private MemberBiz     memberBiz;
    private CmsContentBiz cmsContentBiz;

    public void setMemberBiz(MemberBiz memberBiz) {
        this.memberBiz = memberBiz;
    }

    public void setCmsContentBiz(CmsContentBiz cmsContentBiz) {
        this.cmsContentBiz = cmsContentBiz;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Cookie c1 = new Cookie("timestamp", Config.timestamp);
        Cookie c2 = new Cookie("sign", Config.getSign());
        Cookie c3 = new Cookie("orignalSign", Config.getOriginalSign());
        response.addCookie(c1);
        response.addCookie(c2);
        response.addCookie(c3);

        try {
            //第一次访问检查cookie
            if (request.getSession().getAttribute("member_session") == null) {
                request.getSession().setAttribute("member_session", "true");
                //从cookie里面取值登录
                Cookie[] cookies = request.getCookies();
                String loginName = "";
                String loginPwd = "";
                if (cookies != null) {
                    for (int i = 0; i < cookies.length; i++) {
                        Cookie cookie = cookies[i];
                        if ("guoguojie_user_loginName".equals(cookie.getName())) {
                            loginName = URLDecoder.decode(cookie.getValue(), "utf-8");
                        } else if ("guoguojie_user_loginPwd".equals(cookie.getName())) {
                            loginPwd = URLDecoder.decode(cookie.getValue(), "utf-8");
                        }
                    }
                    if (Common.notEmptyStr(loginName) && Common.notEmptyStr(loginPwd)) {
                        Member member = memberBiz.login(loginName, loginPwd, Common.getIp(request));
                        if (null != member) {
                            request.getSession(true).setAttribute("onlineUser", new OnLineUserListener(member));
                        }
                    }
                }
            }
            List<CmsContent> fList = cmsContentBiz.getContentList("5d575e2b-3b63-4f6c-9b7e-aade27353510", 50);
            request.setAttribute("fList", fList);

            List<CmsContent> footer = cmsContentBiz.getContentList("d74dc9bb-b312-40f7-956f-1f946874ba26", 2);
            request.setAttribute("footer", footer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }

}
